package com.aat.modle.renzhengheyi;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.aat.modle.renzhengheyi.kalala.Base64;
import com.aat.modle.renzhengheyi.kalala.RSAUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * Created by 炒饭 on 2017/10/31.
 */

public class RenzhyApi {
	private static String publickey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3//sR2tXw0wrC2DySx8vNGlqt\n"
			+ "3Y7ldU9+LBLI6e1KS5lfc5jlTGF7KBTSkCHBM3ouEHWqp1ZJ85iJe59aF5gIB2kl\n"
			+ "Bd6h4wrbbHA2XE1sq21ykja/Gqx7/IRia3zQfxGv/qEkyGOx+XALVoOlZqDwh76o\n" + "2n1vP1D+tD3amHsK7QIDAQAB";

	public static void postServer(String name, String idNumber, String idIssued, String startDate, String endDate,
			String token, String appId, String bundleId, String imgBase64) throws Exception {
		String url = "http://10.1.5.195:81/index.php?s=/Home/Api/index";
		CloseableHttpClient client = HttpClients.createDefault();
		HttpPost post = new HttpPost(url);

		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("name", procressData(name, true)));
		nvps.add(new BasicNameValuePair("idNumber", procressData(idNumber, true)));
		nvps.add(new BasicNameValuePair("idIssued", procressData(idIssued, true)));
		nvps.add(new BasicNameValuePair("startDate", procressData(startDate, true)));
		nvps.add(new BasicNameValuePair("endDate", procressData(endDate, true)));

		nvps.add(new BasicNameValuePair("token", procressData(token, true)));
		nvps.add(new BasicNameValuePair("appId", procressData(appId, true)));
		nvps.add(new BasicNameValuePair("platform", procressData("1", true)));
		nvps.add(new BasicNameValuePair("libVersion", procressData("1.0.0", true)));
		nvps.add(new BasicNameValuePair("bundleId", procressData(bundleId, true)));

		nvps.add(new BasicNameValuePair("portrait", imgBase64));

		post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
		CloseableHttpResponse resp = client.execute(post);
		HttpEntity entity = resp.getEntity();
		String res = EntityUtils.toString(entity);
		System.out.println(res);
		JSONObject obj = JSON.parseObject(res);
		
		System.out.println(unicode2String(obj.getString("msg")));

		EntityUtils.consume(entity);
		if (resp != null) {
			resp.close();
		}
		if (post != null) {
			post.releaseConnection();
		}
		if (client != null) {
			client.close();
		}

	}

	private static String procressData(String data, boolean ifNeedRSA) throws Exception {
		if (!ifNeedRSA) {
			return Base64.encode(data.getBytes());
		}
		// 去换行、去空格
		String shrinkPublicString = publickey.replaceAll("\n", "").replaceAll("\\s", "");

		String ReqData = RSAUtils.encryptString(shrinkPublicString, data);

		return ReqData;
	}

	public static String unicode2String(String unicode) {

		StringBuffer string = new StringBuffer();

		String[] hex = unicode.split("\\\\u");

		for (int i = 1; i < hex.length; i++) {

			// 转换出每一个代码点
			int data = Integer.parseInt(hex[i], 16);

			// 追加成string
			string.append((char) data);
		}

		return string.toString();
	}
}
